package com.willcode.demo.webrequest;

import cn.hutool.core.map.MapBuilder;
import cn.hutool.core.net.NetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.support.RequestContext;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author Will
 */
@Slf4j
@RestController
@RequestMapping(name = "测试接口", value = "/springboot/democontroller")
public class DemoController {

    @RequestMapping(value = "getstr4get", method = RequestMethod.GET)
    String getStr4Get(@RequestParam(value = "str") String str) {

        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        String sessionId = requestAttributes.getSessionId();

        log.info("sessionId:{}",sessionId);

        return StrUtil.format("Get方法获取字符串信息，参数str为：{}", str);
    }

    @RequestMapping(value = "getstr4post", method = RequestMethod.POST)
    String getStr4Post(@RequestParam(value = "str") String str) {
        return StrUtil.format("Post方法获取字符串信息，参数str为：{}", str);
    }

    /**
     * RESTFull风格接口
     * @param name
     * @return
     */
    @RequestMapping(value = "getstr4postdemobody", method = RequestMethod.POST)
    Demo getStr4PostDemoBody(@RequestParam(value = "name") String name) {
        Demo demo = new Demo();
        demo.setName(name);
        demo.setAge(18);

        Map<String, Object> build = MapBuilder.<String, Object>create().
                put("head", "head01").
                put("foot", "foot01").build();
        demo.setBody(build);
        return demo;
    }
}
